//
//  StuClockHisVC.m
//  test
//
//  Created by 朱圳 on 2018/3/20.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "StuClockHisVC.h"
#import "ClockDaysCell.h"
#import "StuClockVC.h"
@interface StuClockHisVC ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *jwCheackArray;
@property(nonatomic,strong)NSMutableArray *daysArray;

@end

@implementation StuClockHisVC
static NSString * const reuseIdentifier = @"Cell";
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setData];
    [self.view addSubview:self.collectionView];
    self.title = [NSString stringWithFormat:@"%@-第%@天学习",self.model.name,self.days];
    [self setBack];
}
-(void)setBack{
    /*设置导航栏上面的内容*/
    UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    // 设置图片
    [backBtn setBackgroundImage:[UIImage imageNamed:@"navigationbar_back"] forState:UIControlStateNormal];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"navigationbar_back_highlighted"] forState:UIControlStateHighlighted];
    // 设置尺寸
    backBtn.size = backBtn.currentBackgroundImage.size;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    /**
     解决自定义返回 手势失效
     */
    self.navigationController.interactivePopGestureRecognizer.delegate=(id)self;
    [self requestData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
-(void)setData{
    for (int i = 1; i<366; i++) {
        [self.daysArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.collectionView reloadData];
    });
}
-(void)requestData{
    [self.dataArray removeAllObjects];
    [self.jwCheackArray removeAllObjects];
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{
                           @"jwid" : JWid,
                           @"jwpassword" : JWpassword,
                           @"sid" : self.model.sid
                           };
    [[AFNetworkTool shareManager]postJSONWithUrl:JW_Clockdays parmas:dict successData:^(id json) {
        NSArray *arr = json[@"days"];
        for (int i = 0; i<arr.count; i++) {
            [weakSelf.dataArray addObject:arr[i][@"orderline"]];
            if ([arr[i][@"jwstate"] isEqualToString:@"0"]) {
                [weakSelf.jwCheackArray addObject:arr[i][@"orderline"]];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.collectionView reloadData];
        });
    } failure:^(NSError *error) {
        [Utility showFail:@"网络连接出错..."];
        NSLog(@"error%@",error);
    }];
}
#pragma mark  ---UICollectionView---
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.daysArray.count;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString * stringID = [NSString stringWithFormat:@"CookingEquipmentCellId%ld",indexPath.row];
    ClockDaysCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:stringID forIndexPath:indexPath];
    cell.days.textAlignment = NSTextAlignmentCenter;
    cell.days.text = self.daysArray[indexPath.row];
    if ([cell.days.text isEqualToString:self.days]) {
        
        UIView *xl = [[UIView alloc]init];
        xl.backgroundColor = [UIColor redColor];
        [cell addSubview:xl];
        [xl mas_makeConstraints:^(MASConstraintMaker *make) {
            [make.top.mas_equalTo(cell.days.mas_bottom) setOffset:-8];
            [make.left.mas_equalTo(cell.days.mas_left) setOffset:15];
            [make.right.mas_equalTo(cell.days.mas_right) setOffset:-15];
            make.height.mas_equalTo(3);
        }];
    }
    
    for (int i = 0; i<self.dataArray.count; i++) {
        if ([self.daysArray[indexPath.row] isEqualToString:self.dataArray[i]]) {
            [cell.days setBackgroundColor:HWColor(255,161,136)];
            [cell.days setTextColor:[UIColor whiteColor]];
            for (int j = 0; j<self.jwCheackArray.count; j++) {
                if ([cell.days.text isEqualToString:self.jwCheackArray[j]]) {
                    cell.days.text = [NSString stringWithFormat:@"* %@",cell.days.text];
                }
            }
        }
        
    }

    cell.days.layer.cornerRadius = 12;
    cell.days.clipsToBounds = YES;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    StuClockVC *vc = [[StuClockVC alloc]init];
    vc.days = self.daysArray[indexPath.row];
    vc.sid = self.model.sid;
  [self.navigationController pushViewController:vc animated:YES];
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake((ScreenWidth-26)/7, 40);
}
//边距设置:整体边距的优先级，始终高于内部边距的优先级
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                       layout:(UICollectionViewLayout *)collectionViewLayout
       insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
    //分别为上、左、下、右
}

#pragma mark  ---懒加载---

- (NSMutableArray *)dataArray {
    
    if(!_dataArray){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableArray *)jwCheackArray {
    
    if(!_jwCheackArray){
        _jwCheackArray = [NSMutableArray array];
    }
    return _jwCheackArray;
}

- (NSMutableArray *)daysArray {
    
    if(!_daysArray){
        _daysArray = [NSMutableArray array];
    }
    return _daysArray;
}
-(UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) collectionViewLayout:layout];
        layout.minimumLineSpacing = 1;//设置最小行间距
        layout.minimumInteritemSpacing = 1;//item间距(最小值)
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        for (NSInteger i = 0; i < self.daysArray.count ; i++) {
            
            NSString * stringID = [NSString stringWithFormat:@"CookingEquipmentCellId%ld",i];
            [_collectionView registerNib:[UINib nibWithNibName:@"ClockDaysCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:stringID];
        }
        
    }
    return _collectionView;
}

@end

